import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { ref } from 'vue'

export default function useMiniShare() {
  type shareParamsType = {
    title?: string
    path: string
    imageUrl?: string
    desc?: string
  }
  const shareParams = ref<shareParamsType>({
    title: '麦德范小程序',
    path: '/pages/index/index',
  })
  /** 限制是否开启分享朋友圈 */
  const isShareFriends = (type: boolean = false) => {
    // 开启小程序分享功能
    uni.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
    })
    // console.log('监听分享')
    if (!type) {
      // 关闭朋友圈分享
      wx.hideShareMenu({
        menus: ['shareTimeline'],
      })
    }
  }
  /**修改分享参数 */
  const setupOnShareAppMessage = (params: shareParamsType) => {
    shareParams.value = params
    // console.log('修改分享参数', shareParams.value)
  }
  onShareAppMessage(() => {
    // console.log('调用分享', shareParams.value)
    return shareParams.value
  })
  // 用户点击右上角分享

  // 监听用户分享朋友圈
  onShareTimeline(() => {
    return {
      title: shareParams.value.title,
      imageUrl: shareParams.value?.imageUrl,
    }
  })

  return {
    shareParams,
    isShareFriends,
    onShareTimeline,
    onShareAppMessage,
    setupOnShareAppMessage,
  }
}
